/**
 * 确认以及页面添加字段
 */
$(function () {
    var a = GetRequest();
    var ids = a['id'];
    display(ids);
    /**
     * 页面加载执行省市县
     * @type {string}
     */
    $.ajax({
        url: "../../DropDownBoxQuery/findAddress",
        type: "post",
        dataType: "json",
        success: function (map) {
            //console.log(map);
            var citylist = map.cityList;
            var countylist = map.countyList;
            var provincelist = map.provinceList;
            var province_select = document.getElementById("province");
            var city_select = document.getElementById("city");
            var county_select = document.getElementById("county");
            //消除下拉框
            province_select.innerHTML = "<option value='0'>选择</option>";
            city_select.innerHTML = "<option value='0'>选择</option>";
            county_select.innerHTML = "<option value='0'>选择（区）</option>"
            //加载省份
            for (var i = 0; i < provincelist.length; i++) {
                var opt = document.createElement("option");
                opt.innerText = provincelist[i].name;
                opt.setAttribute("value", provincelist[i].code);
                province_select.appendChild(opt);
            }
            //给省份添加change事件，选择省份，获取城市
            province_select.onchange = function () {
                //获取省份ID
                var provinceCode = province_select.value;
                //先清除原来的城市数据
                city_select.innerHTML = "<option value='0'>选择</option>";
                county_select.innerHTML = "<option value='0'>选择区</option>";
                //根据省份code获取 城市列表(遍历省份去匹配)
                for (var i = 0; i < citylist.length; i++) {
                    if (citylist[i].provincecode == provinceCode) {
                        var citys = citylist[i].name;
                        var opt = document.createElement("option");
                        opt.innerText = citylist[i].name;
                        opt.setAttribute("value", citylist[i].code);
                        city_select.appendChild(opt);
                    }
                }
            }
            //给城市添加change事件，选择城市，获取区县
            city_select.onchange = function () {
                //获取城市ID
                var cityCode = city_select.value;
                //先清除原来的城市数据
                county_select.innerHTML = "<option value='0'>选择区</option>";
                //根据城市ID获取区县列表
                for (var i = 0; i < countylist.length; i++) {
                    if (countylist[i].citycode == cityCode) {
                        var opt = document.createElement("option");
                        opt.innerText = countylist[i].name;
                        opt.setAttribute("value", countylist[i].code);
                        county_select.appendChild(opt);
                    }
                }
            }

            var province_select1 = document.getElementById("province1");
            var city_select1 = document.getElementById("city1");
            var county_select1 = document.getElementById("county1");
            //消除下拉框
            province_select1.innerHTML = "<option value='0'>选择</option>";
            city_select1.innerHTML = "<option value='0'>选择</option>";
            county_select1.innerHTML = "<option value='0'>选择（区）</option>"
            //加载省份
            for (var i = 0; i < provincelist.length; i++) {
                var opt = document.createElement("option");
                opt.innerText = provincelist[i].name;
                opt.setAttribute("value", provincelist[i].code);
                province_select1.appendChild(opt);
            }
            //给省份添加change事件，选择省份，获取城市
            province_select1.onchange = function () {
                //获取省份ID
                var provinceCode = province_select1.value;
                //先清除原来的城市数据
                city_select1.innerHTML = "<option value='0'>选择</option>";
                county_select1.innerHTML = "<option value='0'>选择区</option>";
                //根据省份code获取 城市列表(遍历省份去匹配)
                for (var i = 0; i < citylist.length; i++) {
                    if (citylist[i].provincecode == provinceCode) {
                        var citys = citylist[i].name;
                        var opt = document.createElement("option");
                        opt.innerText = citylist[i].name;
                        opt.setAttribute("value", citylist[i].code);
                        city_select1.appendChild(opt);
                    }
                }
            }
            //给城市添加change事件，选择城市，获取区县
            city_select1.onchange = function () {
                //获取城市ID
                var cityCode = city_select1.value;
                //先清除原来的城市数据
                county_select1.innerHTML = "<option value='0'>选择区</option>";
                //根据城市ID获取区县列表
                for (var i = 0; i < countylist.length; i++) {
                    if (countylist[i].citycode == cityCode) {
                        var opt = document.createElement("option");
                        opt.innerText = countylist[i].name;
                        opt.setAttribute("value", countylist[i].code);
                        county_select1.appendChild(opt);
                    }
                }
            }
        }
    });
    /**
     *  病区楼、楼层、房间、床位
     */
    $.ajax({
        url: "../../DropDownBoxQuery/findWard",
        type: "post",
        dataType: "json",
        success: function (map) {
            var buildingList = map.buildingList;
            var layerList = map.layerList;
            var roomList = map.roomList;
            var bedList = map.bedList;
            var buildingList_select = document.getElementById("buildingList");
            var layerList_select = document.getElementById("layerList");
            var roomList_select = document.getElementById("roomList");
            var bedList_select = document.getElementById("bedList");
            //消除下拉框
            buildingList_select.innerHTML = "<option value='0'>选择病区楼</option>"
            layerList_select.innerHTML = "<option value='0'>选择楼层</option>"
            roomList_select.innerHTML = "<option value='0'>选择病区房号</option>";
            bedList_select.innerHTML = "<option value='0'>选择床位号</option>";
            //加载病区楼
            for (var i = 0; i < buildingList.length; i++) {
                var opt = document.createElement("option");
                opt.innerText = buildingList[i].wardBuildingName;
                opt.setAttribute("value", buildingList[i].wardBuildingCode);
                buildingList_select.appendChild(opt);
            }
            //给病区楼添加change事件，选择病区楼，获取楼层
            buildingList_select.onchange = function () {
                //获取病区楼ID
                var buildingLisCode = buildingList_select.value;
                //先清除原来的病区楼数据
                layerList_select.innerHTML = "<option value='0'>选择楼层</option>"
                roomList_select.innerHTML = "<option value='0'>选择病区房号</option>";
                bedList_select.innerHTML = "<option value='0'>选择床位号</option>";
                //根据病区楼code获取 楼层列表(遍历病区楼去匹配)
                for (var i = 0; i < layerList.length; i++) {
                    if (layerList[i].wardBuildingCode == buildingLisCode) {
                        var opt = document.createElement("option");
                        opt.innerText = layerList[i].wardLayerName;
                        opt.setAttribute("value", layerList[i].wardLayerCode);
                        layerList_select.appendChild(opt);
                    }
                }
            }
            //给楼层添加change事件，选择楼层，获取病区房号
            layerList_select.onchange = function () {
                //获取楼层ID
                var layerCode = layerList_select.value;
                //先清除原来的楼层数据
                roomList_select.innerHTML = "<option value='0'>选择病区房号</option>";
                bedList_select.innerHTML = "<option value='0'>选择床位号</option>";
                //根据区房号ID获取区县列表
                for (var i = 0; i < roomList.length; i++) {
                    if (roomList[i].wardLayerCode == layerCode) {
                        var opt = document.createElement("option");
                        opt.innerText = roomList[i].wardRoomName;
                        opt.setAttribute("value", roomList[i].wardRoomCode);
                        roomList_select.appendChild(opt);
                    }
                }
            }
            //给病区房号添加change事件，选择病区房号，获取床位号
            roomList_select.onchange = function () {
                //获取病区房号ID
                var roomCode = roomList_select.value;
                //先清除原来的城市数据
                bedList_select.innerHTML = "<option value='0'>选择床位号</option>";
                //根据病区房号ID获取区县列表
                for (var i = 0; i < bedList.length; i++) {
                    if (bedList[i].wardRoomCode == roomCode) {
                        var opt = document.createElement("option");
                        opt.innerText = bedList[i].wardBedName;
                        opt.setAttribute("value", bedList[i].wardBedCode);
                        bedList_select.appendChild(opt);
                    }
                }
            }
        }
    });


    /**
     * 保存以及添加
     */
    $("#save").click(function () {
        if (ids.length != 0) {
            insertPlay(ids);
        }
    })

    /**
     * 页面加载执行科室查询
     * @type {string}
     */
    var htmlOption = "";
    $.ajax({
        url: "../../DropDownBoxQuery/findByhospitalId",
        type: "post",
        dataType: "json",
        success: function (map) {
            var departmentList = map.departmentList;
            var department_select = document.getElementById("sel_dempartment");
            //加载
            for (var i = 0; i < departmentList.length; i++) {
                var opt = document.createElement("option");
                opt.innerText = departmentList[i].departmentName;
                opt.setAttribute("value", departmentList[i].departmentId);
                department_select.appendChild(opt);
            }
        }
    });

    /**
     * 页面加载执行民族
     * @type {string}
     */
    var htmlNationalList = "";
    $.ajax({
        url: "../../DropDownBoxQuery/findNational",
        type: "post",
        dataType: "json",
        success: function (map) {
            var nationalList = map.nationalList;
            var national_select = document.getElementById("sel_national");
            //加载
            for (var i = 0; i < nationalList.length; i++) {
                var opt = document.createElement("option");
                opt.innerText = nationalList[i].nationalName;
                opt.setAttribute("value", nationalList[i].nationalId);
                national_select.appendChild(opt);
            }
        }
    })

    function display(ids) {
        var htmlstr = "";
        htmlstr += "<div class='col-sm-12'>";
        htmlstr += "<label class='col-sm-2 control-label' style='color: red;'>新建文件名：</label>";
        htmlstr += "<div class='col-sm-3'>";
        htmlstr += "<input name='createName' id='createName' class='form-control' />";
        htmlstr += "</div>";
        htmlstr += "</div>";
        var ides = ids.split("-");
        for (var i = 0; i < ides.length - 1; i++) {
            var index = ides[i].split(",");
            var columenName = "";   //字段名称
            var columenComment = "";    //注释名称i
            for (var j = 0; j < index.length; j++) {
                if (j == 0) {
                    columenName = index[j];
                } else {
                    columenComment = index[j];
                }
            }
            /*向页面上拼接数据*/
            if (columenName.indexOf("ids") != -1) {
                htmlstr += "<div class='col-sm-12' style='height: 80px; line-height: 80px;'>";
                htmlstr += "<p style='font-size: 20px;'><b>" + columenComment + "</b></p>";
                htmlstr += "</div>";
            } else {
                //展示省市县信息
                if (columenName.indexOf("place_of_birth_id") != -1) {
                    htmlstr += "<div class='form-group col-sm-12' style='font-size: 16px;'>";
                    htmlstr += "<label class='col-sm-1 control-label'>" + columenComment + "</label>";
                    htmlstr += "<div class='col-sm-11'>";
                    htmlstr += "<div class='btn-toolbar' role='toolbar'>";
                    htmlstr += "<div class='btn-group btn-group-xs'>";
                    htmlstr += "<select class='btn btn-default' id='province'>"
                    htmlstr += "</select>";
                    htmlstr += "<input class='btn btn-default' value='省' readonly style='width: 25px;' />";
                    htmlstr += "<select class='btn btn-default' id='city'>"
                    htmlstr += "</select>";
                    htmlstr += "<input class='btn btn-default' value='市' readonly style='width: 25px;' />";
                    htmlstr += "<select class='btn btn-default' id='county'>"
                    htmlstr += "</select>";
                    htmlstr += "<input class='btn btn-default' value='县' readonly style='width: 25px;' />";
                    htmlstr += "<input class='btn btn-default' placeholder='如：新宫家园北区7号楼1单元504室'/>";
                    htmlstr += "</div>";
                    htmlstr += "</div>";
                    htmlstr += "</div>";
                    htmlstr += "</div>";
                } else if (columenName.indexOf("address_id") != -1) {
                    htmlstr += "<div class='form-group col-sm-12' style='font-size: 16px;'>";
                    htmlstr += "<label class='col-sm-1 control-label'>" + columenComment + "</label>";
                    htmlstr += "<div class='col-sm-11'>";
                    htmlstr += "<div class='btn-toolbar' role='toolbar'>";
                    htmlstr += "<div class='btn-group btn-group-xs'>";
                    htmlstr += "<select class='btn btn-default' id='province1'>"
                    htmlstr += "</select>";
                    htmlstr += "<input class='btn btn-default' value='省' readonly style='width: 25px;' />";
                    htmlstr += "<select class='btn btn-default' id='city1'>"
                    htmlstr += "</select>";
                    htmlstr += "<input class='btn btn-default' value='市' readonly style='width: 25px;' />";
                    htmlstr += "<select class='btn btn-default' id='county1'>"
                    htmlstr += "</select>";
                    htmlstr += "<input class='btn btn-default' value='县' readonly style='width: 25px;' />";
                    htmlstr += "<input class='btn btn-default' placeholder='如：新宫家园北区7号楼1单元504室'/>";
                    htmlstr += "</div>";
                    htmlstr += "</div>";
                    htmlstr += "</div>";
                    htmlstr += "</div>";
                } else  //展示病区（病区楼、楼层、房间、床位）信息
                if (columenName.indexOf("ward_id") != -1) {
                    htmlstr += "<div class='form-group col-sm-12' style='font-size: 16px;'>";
                    htmlstr += "<label class='col-sm-1 control-label'>" + columenComment + "</label>";
                    htmlstr += "<div class='col-sm-11'>";
                    htmlstr += "<div class='btn-toolbar' role='toolbar'>";
                    htmlstr += "<div class='btn-group btn-group-xs'>";
                    htmlstr += "<select class='btn btn-default' id='buildingList'>"
                    htmlstr += "</select>";
                    htmlstr += "<input class='btn btn-default' value='楼' readonly style='width: 50px;' />";
                    htmlstr += "<select class='btn btn-default' id='layerList'>"
                    htmlstr += "</select>";
                    htmlstr += "<input class='btn btn-default' value='楼层' readonly style='width: 50px;' />";
                    htmlstr += "<select class='btn btn-default' id='roomList'>"
                    htmlstr += "</select>";
                    htmlstr += "<input class='btn btn-default' value='房间' readonly style='width: 50px;' />";
                    htmlstr += "<select class='btn btn-default' id='bedList'>"
                    htmlstr += "</select>";
                    htmlstr += "<input class='btn btn-default' value='床位' readonly style='width: 50px;' />";
                    htmlstr += "</div>";
                    htmlstr += "</div>";
                    htmlstr += "</div>";
                    htmlstr += "</div>";
                } else {
                    htmlstr += "<div class='form-group col-sm-6' style='font-size: 16px;'>";
                    htmlstr += "<label class='col-sm-3 control-label'>" + columenComment + "</label>";
                    htmlstr += "<div class='col-sm-5'>";
                }
                if (columenName.lastIndexOf("id") == -1) {  //文本框
                    if (columenName.indexOf("date") == -1 && columenName.indexOf("days") == -1 && columenName.indexOf("time") == -1) {
                        htmlstr += "<input name='" + columenName + "' class='form-control' />";
                    } else {
                        htmlstr += "<input type='datetime-local' name='" + columenName + "' class='form-control' />";
                    }
                    htmlstr += "</div>";
                } else {  //下拉框
                    if (columenName.indexOf("department") != -1 || columenName.indexOf("departments") != -1) {
                        htmlstr += "<select name='" + columenName + "' class='form-control' id='sel_dempartment'></select>";
                    } else if (columenName.indexOf("national") != -1) {
                        htmlstr += "<select name=" + columenName + " class='form-control' id='sel_national'></select>";
                    } else if (columenName.indexOf("place_of_birth_id") != -1 || columenName.indexOf("address_id") != -1) {
                        //因为地址（需要省市县，乡区楼等详细地址）所以需要占用一行，不能写在这
                    } else if (columenName.indexOf("ward_id") != -1) {
                        //因为病区（病区楼、楼层、房间、床位）信息）所以需要占用一行，不能写在这
                    } else {
                        htmlstr += "<select name='" + columenName + "' class='form-control'></select>";
                    }
                    htmlstr += "</div>";
                }
            }
            htmlstr += "</div>";
        }
        $("#add_from").html(htmlstr);
    }

    /**
     * 保存数据到数据库中
     * @param ids
     */
    function insertPlay(ids) {
        var title = []; //标题
        var field_name = [];    //字段名
        var type_of_frame = []; //框的类型
        var contents_of_the_box = []; //框的内容
        var createName = $("#createName").val();
        var ides = ids.split("-");
        for (var i = 0; i < ides.length; i++) {
            var index = ides[i].split(",");
            var columenName = "";   //字段名称
            var columenComment = "";    //注释名称
            for (var j = 0; j < index.length; j++) {
                if (j == 0) {
                    columenName = index[j];
                } else {
                    columenComment = index[j];
                }
            }
            /*插入数据库中拼接*/
            if (columenName.lastIndexOf("ids") != -1) {
                title.push(columenComment); //标题
                field_name.push(columenName + "-" + columenComment);    //字段名
                type_of_frame.push("无"); //框的类型
                contents_of_the_box.push("无"); //框的内容
            } else {
                if (columenName.lastIndexOf("id") != -1) {  //下拉框
                    title.push("无"); //标题
                    field_name.push(columenName + "-" + columenComment);    //字段名
                    type_of_frame.push("select"); //框的类型
                    contents_of_the_box.push("无"); //框的内容
                }/* else if( columenName.lastIndexOf("fo_group_state") != -1 ){//是否加入随访组
                    title.push("无"); //标题
                    field_name.push(columenName + "-" + columenComment);    //字段名
                    type_of_frame.push("select"); //框的类型
                    contents_of_the_box.push("无"); //框的内容
                } else if( columenName.lastIndexOf("gender") != -1 ){//性别
                    title.push("无"); //标题
                    field_name.push(columenName + "-" + columenComment);    //字段名
                    type_of_frame.push("select"); //框的类型
                    contents_of_the_box.push("无"); //框的内容
                } */ else {  //文本框
                    title.push("无"); //标题
                    field_name.push(columenName + "-" + columenComment);    //字段名
                    type_of_frame.push("input"); //框的类型
                    contents_of_the_box.push("无"); //框的内容
                }
            }
        }
        /*保存到数据库中的Ajax*/
        $.ajax({
            url: "../../template/insertTemplate",
            type: "post",
            data: 'title=' + title + '&field_name=' + field_name + '&type_of_frame=' + type_of_frame + '&contents_of_the_box=' + contents_of_the_box + '&createName=' + createName,
            dataType: "json",
            success: function (map) {
                alert("保存成功！！");
                location.href="/page/template/general_template.jsp";
            }
        })
    }

    /**
     * 通过路径获取id
     * @returns
     */
    function GetRequest() {
        var url = location.search; //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }

})
